<?php

require_once("model/forecast.class.php");
require_once("model/forecastDays.class.php");

class forecastDao {
	
	public function getWeather($lat, $long){
		$file = 'storage/objects/lat'.$lat.'long'.$long.'.obj';
		$diff = -1;
		
		if(file_exists($file)){
			$diff = time() - filemtime($file);
		}
		
		if ($diff > 0 && $diff < 900){
			$handle = fopen($file, 'rb');
			$string = fread($handle, filesize($file));
			$obj = unserialize($string);
			return $obj;
		} else {
			$obj = forecastDao::getXMLStream($lat,$long);
			$string = serialize($obj);
			$handle = fopen($file,'w');
			if (!fwrite($handle,$string)){
				echo '<p class="error">Klarer ikke å skrive til '.$file.'</p>';	
			}
		}
		return $obj;
	}
	
	private function getXMLStream($lat, $long){
		//$url = 'http://api.yr.no/weatherapi/locationforecast/1.4/?lat=60.10;lon=9.58';
		
		$doc = new DOMDocument('1.0');
		$file = 'storage/forecast/lat'.$lat.'long'.$long.'.xml';
		$diff = -1;
		
		if(file_exists($file)){
			$diff = time() - filemtime($file);
		}
		
		if ($diff > 0 && $diff < 900){
			$doc->load($file);
		} else {
			$url = 'http://api.yr.no/weatherapi/locationforecast/1.4/?lat='.$lat.';lon='.$long;
			$doc->load($url);
			$doc->save($file);
		}
		
		$xpath = new DOMXpath( $doc );
		
		$foreCastDays = new ForecastDaysModel();		
		$today = date_create();
		
		for($i=0;$i<8;$i++){
			$date = $today->format("Y-m-d");
			for ($j=0;$j<24;$j++){
				if ($j < 10){
					$j = '0'.$j;
				}
				$query = '/weatherdata/product/time[@from="'.$date.'T'.$j.':00:00Z"]/location';
				//$result = forecastDao::xPathQuery($doc, $query);
				$result = $xpath->query($query);
				if ($result->length > 0){
//					$hour = $j-2;
//					$day = $i;
//					if ($hour<0){
//						$day--;
//						$hour+=24;
//					}
					$foreCastDays->setForecast($i, $j, forecastDao::getObject($result));
				//	if ($result->length < 6){
				//		echo $result->length.' : from="'.$date.'T'.$j.':00:00Z<br/>';
				//	}
				}
			//	echo $result->length . "<br>";
			}
			
			$today->modify("+1 days");
			
			
			
		}
		
		return $foreCastDays;
		//$query = '/weatherdata/product/time[@from="'.functions->generateXMLDate().'"]/location';
		//return forecastDao::xPathQuery($url, $query);

	}
	
	private function getObject($result){
		
		//$xmluri = 'http://api.yr.no/weatherapi/locationforecast/1.4/?lat='.$lat.';lon='.$long;
		//$xmluri = 'http://api.yr.no/weatherapi/locationforecast/1.4/?lat=60.10;lon=9.58';
		//$xmluri = 'dao/api.yr.no.xml'; //  <-- xml-fil i tilfelle uri'en over ikke fungerer

				
//		$xpath = new DOMXpath( $doc );
//		$result = $xpath->query($query);
		$obj = new forecastModel();
		
	//	$times = array();
		
		//if (!is_null($result->item(0))){
		
		// hente ut for hvert klokkeslett
	//	echo $itr = $result->length;
	//	echo "<br/>";
		
		
	/*	
		for ($i=0; $i<$itr; $i++){
			// hente dato og tidspunkt, opprette forecastDays-obj med forecasts inni
			$from = $result->item($i)->getAttribute('from');
			//$to   = $result->item($i)->getAttribute('to');
			$year = explode('-',$from);
			
		}
	*/
		if ($result->length >= 1){
		// item 0
		$obj->setWindDir($result->item(0)->getElementsByTagName("windDirection")->item(0)->getAttribute("deg"));
		$obj->setWindSpeed($result->item(0)->getElementsByTagName("windSpeed")->item(0)->getAttribute("beaufort"));
		$obj->setWindSpeedTxt($result->item(0)->getElementsByTagName("windSpeed")->item(0)->getAttribute("name"));
		$obj->setTemperature($result->item(0)->getElementsByTagName("temperature")->item(0)->getAttribute("value"));
		$obj->setPressure($result->item(0)->getElementsByTagName("pressure")->item(0)->getAttribute("value"));
		$obj->setCloudiness($result->item(0)->getElementsByTagName("cloudiness")->item(0)->getAttribute("percent"));
		$obj->setFog($result->item(0)->getElementsByTagName("fog")->item(0)->getAttribute("percent"));
		//$obj->setLowClouds($result->item(0)->getElementsByTagName("windDirection")->item(0)->getAttribute("deg"));
		}
	
		if ($result->length >=2 )
			$obj->setPrecipitation($result->item(1)->getElementsByTagName("precipitation")->item(0)->getAttribute("value"));
		if ($result->length >= 3)
			$obj->setWeatherSymbol($result->item(2)->getElementsByTagName("symbol")->item(0)->getAttribute("number"));
	/*	if ($result->length >= 4)
			$obj->setWeatherSymbol($result->item(3)->getElementsByTagName("symbol")->item(0)->getAttribute("number"));
		if ($result->length >= 5)
			$obj->setWeatherSymbol($result->item(4)->getElementsByTagName("symbol")->item(0)->getAttribute("number"));
		if ($result->length >= 6)
			$obj->setWeatherSymbol($result->item(5)->getElementsByTagName("symbol")->item(0)->getAttribute("number"));
	*/	
		
		return $obj;
		}
		/*else {
			echo "<pre>";
			print_r($result);
			echo "</pre><p>Se forecast.dao.php</p>";
			return null;
		}
	}*/
	
}

?>
